<template>
    <div id='app'>
      <el-container class="home_container">
          <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
             <el-button type='info' @click="logout">退出</el-button>        
        </el-header>
        <!-- 页面主体区 -->
        <el-container>
            <!-- 左侧菜单栏 -->
            <el-aside :width="iscollapse?'64px':'200px'">
                <div class="toggle-botton" @click="toggleCollapse">|||</div>
                <el-menu background-color="#333744" router :collapse-transition="false" 
                :collapse="iscollapse" text-color="#fff" active-text-color="#409eff" 
                :unique-opened="true" :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
                          <!-- 以及菜单的模板区域 -->
                        <template slot="title">
                            <i :class="iconObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children"
                         :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                         </el-menu-item>
                    </el-submenu>
                 </el-menu>
            </el-aside>
            <!-- 右侧内容区 -->
            <el-main>
              <!-- 路由占位符 -->
              <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
    </div>
</template>

<script>
export default {
    data(){
        return{
              // 左侧菜单数据
             menulist:[],
             iconObj:{
                "125":'iconfont icon-users',
                "103":'iconfont icon-tijikongjian',
                "101":'iconfont icon-shangpin',
                "102":'iconfont icon-danju',
                "145":'iconfont icon-baobiao',
             },
            //  菜单是否折叠
             iscollapse:false,
            //  激活菜单的地址
             activePath:''
        }
      
    },
    created(){
        // 调用获取菜单的方法
        this.getMenuList(),
        // 刷新页面的时候获取当前激活的菜单项，实际开发中这里应该不会用到
        this.activePath=window.sessionStorage.getItem('activePath')
    },
    methods:{
        // 退出功能
        logout(){
            window.sessionStorage.clear();
            this.$router.push('/login')
        },
        // 获取所有的菜单
        async getMenuList(){
         const {data:res} = await this.$http.get('menus')
        //  console.log(res)
        if(res.meta.status!==200) return this.$message.error(res.meta.msg)
        this.menulist=res.data
        },
        // 点击按钮切换菜单的折叠与展开
        toggleCollapse(){
            this.iscollapse=!this.iscollapse
        },
        // 保存菜单子选项点击时的激活状态
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath=activePath
        }
    }
}
</script>

<style lang="less" scoped>
    .home_container{
        height: 100%;
    }
    .el-header{
        display: flex;
        justify-content: space-between;
        background-color: #373d41;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            > span {
                margin-left: 15px
                
                ;
            }
        }
    }
    .el-aside{
        background-color: #333744;
        .el-menu{
            border-right: 0;
        }
    }
    .el-main{
        background-color: #eaedf1;
    }
    .iconfont{
        margin-right: 10px;
    }
    .toggle-botton{
        background-color: #4a5064;
        color: #fff;
        font-size: 10px;
        text-align: center;
        line-height: 24px;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>